Ext.BLANK_IMAGE_URL = 'styles/images/default/s.gif';
var cmApp;
// 登录窗口
var loginWin = new CQCIS.Window({
    id: 'loginWin',
    title: '登录窗口',
    width: 240,
    height: 130,
    draggable: false,
    resizable: false,
    closable: false,
    buttons: [{
        text: '登录',
        handler: function() {
            var form = loginWin.getBasicForm();
            if (form.isValid()) {
                var values = form.getValues();
                loginDwr.login(values.logincode, values.password, function(result) {
                    if (result.loginState == 1) {
                        loginWin.hide();
                        initFun(result.userDetails);
                    } else {
                        Ext.Msg.alert('登录失败', result.message);
                        form.reset();
                        form.isValid();
                    }
                });
            } else {
                CQCIS.msg('表单中的字段未通过效验，请检查后再提交！');
            }
        }
    }],
    items: new CQCIS.form.DWRForm({
        dwr: {
            load: Ext.emptyFn,
            save: Ext.emptyFn
        },
        items: [{
            fieldLabel: '用户名称',
            name: 'logincode',
            allowBlank: false,
            value: 'ADMIN',
            validator: function(v) {
                return (/[\W]/).test(v) ? "用户名只能由英文字母、数字和下划线组成！" : true;
            }
        }, {
            fieldLabel: '用户密码',
            name: 'password',
            allowBlank: false,
            inputType: 'password',
            enableKeyEvents: true,
            value: '000000',
            listeners: {
                'keypress': function(o, e) {
                    if (e.getKey() == e.ENTER) {
                        loginWin.buttons[0].handler();
                    }
                }
            }
        }]
    })
});

// 修改密码窗口
var pwdWin = new CQCIS.Window({
    id: 'sm_main_pwdWin',
    title: '修改密码',
    width: 240,
    height: 155,
    draggable: false,
    resizable: false,
    items: new CQCIS.form.DWRForm({
        dwr: {
            load: Ext.emptyFn,
            save: function(values) {
                loginDwr.changePwd(values.preWorkerPwd, values.workerPwd, function(result) {
                    if (result.state == 1) {
                        loginDwr.logOut(function() {
                            pwdWin.hide();
                            Ext.Msg.alert('提示', '密码修改成功，请以新的密码登录系统！', function() {
                                location.replace('');
                            });
                        });
                    } else {
                        CQCIS.msg(result.message);
                        pwdWin.getBasicForm().reset();
                        pwdWin.getBasicForm().isValid();
                    }
                    pwdWin.getForm().afterRemote();
                });
            }
        },
        items: [{
            fieldLabel: '当前密码',
            name: 'preWorkerPwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            minLengthText: '密码不得少于6位'
        }, {
            fieldLabel: '新 密 码',
            name: 'workerPwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            minLengthText: '密码不得少于6位',
            regex: /^(?![a-zA-Z]+$)(?![0-9]+$)[a-zA-Z0-9]{6,}$/,
            regexText: '输入的密码必须为<font color="red">数字与字母</font>的组合。',
            validator: function(val) {
                if (pwdWin.getBasicForm().getValues().preWorkerPwd == val) {
                    return '新密码不能与当前密码相同!';
                } else {
                    return true;
                }
            }
        }, {
            fieldLabel: '重复密码',
            name: 'reWorkerPwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            minLengthText: '密码不得少于6位',
            validator: function(val) {
                if (pwdWin.getBasicForm().getValues().workerPwd != val) {
                    return '两次输入的密码不一致!';
                } else {
                    return true;
                }
            }
        }]
    })
});

var initFun = function(user) {
    Ext.get('modiinfo').dom.innerHTML = user.staffName;
    
	var panel = new Ext.Panel({
        title: '首页',
        id: 'welcome_Tab',
        //html: '<div style=" background:url(styles/images/back.jpg) no-repeat; width:1200px; height:901px;"> </div>'
        layout: 'fit'
    });
    Ext.Ajax.request({
		method: 'GET',
		url: 'welcome.js',
		success: function(response) {
			var Module = CQCIS.module;//eval(response.responseText);
			new Module(panel);
			doInit(panel);
		}
	});
	
	var doInit = function(welcomeTab) {
        var center = new Ext.TabPanel({
	        region: 'center',
	        contentEl: 'center',
	        activeItem: 0,
	        deferredRender: false,
	        enableTabScroll: true,
	        monitorResize: true,
	        margins: '0 0 2 2',
	        cmargins: '0 0 2 2',
	        items: welcomeTab
	    });
	    
	    var west = new Ext.tree.TreePanel({
	        rootVisible: false,
	        region: 'west',
	        id: 'west-panel',
	        title: '系统菜单',
	        useArrows: true,
	        animCollapse: false,
	        stateful: false,
	        width: 180,
	        autoScroll: true,
	        collapsible: true,
	        margins: '0 0 2 0',
	        cmargins: '0 0 2 0',
	        loader: new CQCIS.tree.DWRTreeLoader({
	            dwrCall: menuDwr.getMenus,
	            nodeMeta: {
	                id: 'menuId',
	                text: 'menuName',
	                leaf: 'menuIdend'
	            }
	        }),
	        root: {
	            nodeType: 'async',
	            text: '系统菜单',
	            draggable: false,
	            id: 10000,
	            menuId: 10000,
	            menuName: '系统菜单',
	            leaf: false
	        },
	        listeners: {
	            'click': function(node) {
	                if (node.attributes.leaf == 1) {
	                    var moduleId = 'tab_' + node.attributes.menuId;
	                    if (cmApp.body.findById(moduleId)) {
	                        cmApp.body.findById(moduleId).show();
	                        return;
	                    }
	                    
	                    cmApp.loadMask.show();
	                    var panel = cmApp.body.add({
	                        id: moduleId,
	                        title: node.attributes.menuName,
	                        closable: true,
	                        layout: 'fit'
	                    }).show();
	                    Ext.Ajax.request({
	                        method: 'GET',
	                        url: '' + node.attributes.menuUrl,
	                        success: function(response) {
	                            var module = eval(response.responseText);
	                            cmApp[moduleId] = new module(panel);
	                            cmApp.loadMask.hide();
	                        }
	                    });
	                }
	            }
	        }
	    });
	    
	    var north = new Ext.BoxComponent({
	        region: 'north',
	        el: 'north',
	        height: 40
	    });
	    
	    var south = new Ext.Panel({
	        region: 'south',
	        el: 'south',
	        autoScroll: true,
	        height: 200,
	        minSize: 100,
	        maxSize: 300,
	        collapsible: true,
	        animCollapse: false,
	        collapsed: true,
	        collapsedTitle: '系统提示信息',
	        title: '系统提示信息',
	        margins: '0',
	        cmargins: '0'
	    });
    
	    cmApp = new CQCIS.app(north, south, west, center);
	    
	    var loading = Ext.get('loading');
	    var mask = Ext.get('loading-mask');
	    mask.setOpacity(0.8);
	    mask.shift({
	        xy: loading.getXY(),
	        width: loading.getWidth(),
	        height: loading.getHeight(),
	        remove: true,
	        duration: 1,
	        opacity: 0.3,
	        easing: 'bounceOut',
	        callback: function() {
	            loading.fadeOut({
	                duration: 0.2,
	                remove: true
	            });
	        }
	    });
	    
	    Ext.get('modify').on('click', function() {
	        pwdWin.show();
	    });
	    Ext.get('logout').on('click', function() {
	        Ext.Msg.confirm('确认', '您确定要退出本系统吗？', function(para) {
	            if (para == 'yes') {
	                loginDwr.logOut(function() {
	                    location.replace('index.html');
	                });
	            }
	        });
	    });
    };
};

Ext.onReady(function() {
    Ext.QuickTips.init();
    loginDwr.getSessionUser({
        callback: function(user) {
            if (user.loginId !== undefined && user.loginId !== null) {
                initFun(user);
            } else {
                loginWin.show();
            }
        }
    });
});


//跳转功能点
var tzCommonFun = function(tabName, tabTitle, tabUrl){
	if (cmApp.center.findById(tabName)) {
		cmApp.center.findById(tabName).show();
		return;
	}
	var panelTZ = cmApp.center.add({
		id : tabName,
		title : tabTitle,
		closable : true,
		layout : 'fit'
	}).show();
	Ext.Ajax.request({
		method : 'GET',
		url : '' + tabUrl,
		success : function(response) {
			var module = eval(response.responseText);
			CQCIS.app[tabName] = new module(panelTZ);
		}
	});
}
